﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tauren.Business.DLL.Resouces;
using Tauren.Business.DLL.WorkBenchDLL;
using Tauren.Business.Entity.Config;
using Tauren.Business.Entity.Const;
using Tauren.Business.Entity.Enum;
using Tauren.Business.Entity.Model;
using Tauren.Business.Infrastructure.Globals;

namespace Tauren.Business.Service.WorkBenchBLL.V1
{
    public class WorkBenchService : WorkBenchBase
    {
        private IWorkBenchBusiness business;
        public WorkBenchService(IWorkBenchBusiness baseDLL, IConnectionBLLBase connectionBLL, IMenuResourcesBusiness resourcesBusiness) : base(baseDLL, connectionBLL, resourcesBusiness)
        {
            business = baseDLL;
            this.ResourcesInfo = this.GetResources(GlobalConst.RESOURCES_FLAG_MODULE_WORKBENCH_KEY);

        }
        public override double Version => 1.0;

        /// <summary>
        /// 添加工作台
        /// </summary>
        /// <param name="_WorkBenches"></param>
        /// <returns></returns>
        public override IResponseMessage AddWorkBenchs(List<Flow_WorkBench> _WorkBenches)
        {
            this.JudePrmission(GlobalConst.RESOURCES_FLAG_MODULE_FLOWCENTER_KEY.ToLower());
            if (!HavePermission) return ErrorType.BUSINESS_WITHOUT_PERMISSION.Failure(this.CommonText(ErrorType.BUSINESS_WITHOUT_PERMISSION));
            if (_WorkBenches is null || !_WorkBenches.Any()) return GlobalErrorType.GLOBAL_NOT_FOUND_INPUT_DATA.Failure(this.CommonText(GlobalErrorType.GLOBAL_NOT_FOUND_INPUT_DATA));
            List<WorkBench> workBenches = new List<WorkBench>();
            int i = 1;
            _WorkBenches.Where(o => o.Selected).ToList().ForEach(item =>
            {
                workBenches.Add(new WorkBench()
                {
                    ModuleCode = GlobalConst.RESOURCES_FLAG_MODULE_FLOWCENTER_KEY.ToLower(),
                    Code = item.Code,
                    EmpId = Global.EmpId,
                    Type = (int)ModultType.flowcenter,
                    Sequence = i,
                    Creatdate = DateTime.Now,
                    Creator = Global.EmpId,
                    Modifdate = DateTime.Now,
                    Modifier = Global.EmpId
                });
                i++;
            });
            if (business.AddFlowWorkBenches(workBenches))
                return "".OnSucceed(this.CommonText(GlobalErrorType.GLOBAL_SAVE_SUCCESSINFO.ToString()));
            else return GlobalErrorType.GLOBAL_SAVE_FAILURE.Failure(this.CommonText(GlobalErrorType.GLOBAL_SAVE_FAILURE));
        }

        /// <summary>
        /// 获取所有流程
        /// </summary>
        /// <returns></returns>
        public override IResponseMessage GetAllWorkBenchs()
        {
            this.JudePrmission(GlobalConst.RESOURCES_FLAG_MODULE_FLOWCENTER_KEY.ToLower());
            List<Flow_WorkBench> _WorkBenches = new List<Flow_WorkBench>();
            if (HavePermission)
            {
                List<WorkFlowInfo> workFlowInfos = business.GetWorkFlowInfos();
                List<WorkBench> benches = business.GetFlowWorkBenches();
                if (workFlowInfos.Any())
                {
                    if (benches.Any())
                    {
                        if (workFlowInfos.Any())
                        {
                            _WorkBenches = benches.Join(workFlowInfos, a => long.Parse(a.Code), b => b.FlowId, (a, b) => new Flow_WorkBench()
                            {
                                Code = a.Code,
                                EmpId = a.EmpId,
                                CNName = b.CNName,
                                ENName = b.ENName,
                                FlowIcon = b.FlowIcon,
                                FlowId = b.FlowId,
                                FlowStyle = b.FlowStyle,
                                FlowVersionId = b.FlowVersionId,
                                FormId = b.FormId,
                                FormVersionId = b.FormVersionId,
                                Placeholder = b.Placeholder,
                                Sequence = a.Sequence,
                                ModuleCode = a.ModuleCode,
                                Selected = true
                            }).OrderBy(o => o.Sequence).ToList();
                        }
                    }
                    double index = !_WorkBenches.Any() ? 1 : _WorkBenches.Max(o => o.Sequence) + 1;
                    workFlowInfos.Where(o => !benches.Any(b => long.Parse(b.Code) == o.FlowId)).OrderBy(o => o.Sequence).ToList().ForEach(a =>
                    {
                        _WorkBenches.Add(new Flow_WorkBench()
                        {
                            Code = a.FlowId.ToString(),
                            EmpId = Global.EmpId,
                            CNName = a.CNName,
                            ENName = a.ENName,
                            FlowIcon = a.FlowIcon,
                            FlowId = a.FlowId,
                            FlowStyle = a.FlowStyle,
                            FlowVersionId = a.FlowVersionId,
                            FormId = a.FormId,
                            FormVersionId = a.FormVersionId,
                            Placeholder = a.Placeholder,
                            Sequence = index,
                            ModuleCode = GlobalConst.RESOURCES_FLAG_MODULE_FLOWCENTER_KEY.ToLower(),
                        });
                        index++;
                    });
                }
            }
            return _WorkBenches.OnSucceed();
        }

        /// <summary>
        /// 获取该员工的工作台
        /// </summary>
        /// <returns></returns>
        public override IResponseMessage GetWorkBenchs()
        {
            this.JudePrmission(GlobalConst.RESOURCES_FLAG_MODULE_FLOWCENTER_KEY.ToLower());
            List<Flow_WorkBench> _WorkBenches = new List<Flow_WorkBench>();
            if (HavePermission)
            {
                List<WorkBench> benches = business.GetFlowWorkBenches();
                if (benches.Any())
                {
                    List<WorkFlowInfo> workFlowInfos = business.GetWorkFlowInfos(benches.Where(o => o.Type == (int)ModultType.flowcenter && o.ModuleCode.ToLower() == GlobalConst.RESOURCES_FLAG_MODULE_FLOWCENTER_KEY.ToLower()).Select(o => long.Parse(o.Code)).ToArray());
                    if (workFlowInfos.Any())
                    {
                        _WorkBenches = benches.Join(workFlowInfos, a => long.Parse(a.Code), b => b.FlowId, (a, b) => new Flow_WorkBench()
                        {
                            Code = a.Code,
                            EmpId = a.EmpId,
                            CNName = b.CNName,
                            ENName = b.ENName,
                            FlowIcon = b.FlowIcon,
                            FlowId = b.FlowId,
                            FlowStyle = b.FlowStyle,
                            FlowVersionId = b.FlowVersionId,
                            FormId = b.FormId,
                            FormVersionId = b.FormVersionId,
                            Placeholder = b.Placeholder,
                            Sequence = a.Sequence,
                            ModuleCode = a.ModuleCode
                        }).ToList();
                    }
                }
            }
            return _WorkBenches.OnSucceed();
        }
    }
}
